﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Resources;
using ClassLibrary;

namespace ControlLibrary
{
    public partial class EventItem : UserControl
    {
        private Guid _id;
        private Color _selectionBackColor = Color.LightGray;
        private Color _defaultForeColor = Color.Black;
        private Color _linkedForeColor = Color.SteelBlue;

        public Guid ID
        {
            get { return _id; }
        }

        public int ContentWidth
        {
            get
            {
                return label_Date.Width + label_Event.Width;
            }
        }

        public int highlightWidth
        {
            set { panel_HighLight.Width = value; }
        }

        public bool isNewEventLabel;

        public delegate void OnSelect(Guid ID);

        [Category("Configuration"), Browsable(true), Description("")]
        public event OnSelect onSelect;

        [Category("Configuration"), Browsable(true), Description("")]
        public event OnSelect onDelete;

        #region CONSTRUCTEURS

        //If item already exists
        public EventItem(Guid id, string date, string titre, bool lien)
        {
            InitializeComponent();
            label_Date.Visible = true;
            set(date, titre, lien);
            _id = id;
            panel_HighLight.Width = ContentWidth;
            isNewEventLabel = false;
        }

        //New Item
        public EventItem()
        {
            InitializeComponent();
            label_Date.Text = string.Empty;

            label_Event.Font = new System.Drawing.Font(label_Event.Font, FontStyle.Underline);
            panel_HighLight.Width = ContentWidth;
            contextMenuStrip1.Dispose();//do not delete this item
            isNewEventLabel = true;
        }

        #endregion

        public void UpdateUI()
        {
            ResourceManager _RM = new ResourceManager("ControlLibrary.EventItem", typeof(CategoryList).Assembly);
            try
            {
                if(isNewEventLabel)
                    label_Event.Text = _RM.GetString("label_Event.Text");
                toolStripMenuItem_Delete.Text = _RM.GetString("toolStripMenuItem_Delete.Text");
            }
            catch (Exception e)
            {
                ErrorManager.WriteLog("CategoryList", "UpdateUI", e, Parameters.Values.RM.GetString("MB_Error_UpdateUI"));
            }
        }

        #region UI

        private void enter()
        {
            this.Cursor = Cursors.Hand;

            label_Date.BackColor = _selectionBackColor;
            label_Event.BackColor = _selectionBackColor;
            panel_HighLight.BackColor = _selectionBackColor;
        }

        private void leave()
        {
            this.Cursor = Cursors.Default;

            label_Date.BackColor = Color.White;
            label_Event.BackColor = Color.White;
            panel_HighLight.BackColor = Color.White;
        }

        private void Label_MouseEnter(object sender, EventArgs e)
        {
            enter();
        }

        private void Label_MouseLeave(object sender, EventArgs e)
        {
            leave();
        }

        private void UserControl_MouseEnter(object sender, EventArgs e)
        {
            //enter();
        }

        private void UserControl_MouseLeave(object sender, EventArgs e)
        {
            //leave();
        }

        private void panel_HighLight_MouseEnter(object sender, EventArgs e)
        {
            enter();
        }

        private void panel_HighLight_MouseLeave(object sender, EventArgs e)
        {
            leave();
        }

        #endregion

        #region EVENTS

        private void Label_Click(object sender, EventArgs e)
        {
            if (null != onSelect)
                onSelect(_id);
        }

        private void UserConstrol_Click(object sender, EventArgs e)
        {
            /*if (null != onSelect)
                onSelect(_id);*/
        }

        private void panel_HighLight_Click(object sender, EventArgs e)
        {
            if (null != onSelect)
                onSelect(_id);
        }

        private void contextMenuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (null != onDelete)
                onDelete(ID);
        }

        #endregion

        public void set(string date, string titre, bool lien)
        {
            label_Date.Text = date;
            label_Event.Text = titre;
            if (!lien)
            {
                label_Event.ForeColor = _defaultForeColor;
            }
            else label_Event.ForeColor = _linkedForeColor;
        }
    }
}
